define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var groupId = Config.groupId;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'finance/spendinglog/index' + location.search,
                    add_url: 'finance/spendinglog/add',
                    edit_url: 'finance/spendinglog/edit',
                    del_url: 'finance/spendinglog/del',
                    multi_url: 'finance/spendinglog/multi',
                    import_url: 'finance/spendinglog/import',
                    table: 'finance_spending_log',
                }
            });

            var table = $("#table");

            //修改快速搜索显示文字
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function () { return "搜索：ID、金额、事项" };

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                search: true,
                showToggle: false,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), operate: false, sortable: true },
                        { field: 'group_id', title: __('Group.name'), visible: false, addclass: 'selectpage', align: "left", extend: 'data-source="auth/group" data-field="name"', formatter: Table.api.formatter.search },
                        { field: 'subjects_id', title: __('Subjects.name'), visible: false, addclass: 'selectpage', align: "left", extend: 'data-source="finance/subjects/index" data-field="name"', formatter: Table.api.formatter.search },
                        { field: 'type_id', title: __('Type.name'), visible: false, addclass: 'selectpage', align: "left", extend: 'data-source="finance/spending/index" data-field="name"', formatter: Table.api.formatter.search },
                        { field: 'create_id', title: __('Admin.nickname'), visible: false, addclass: 'selectpage', align: "left", extend: 'data-source="auth/admin/index" data-field="nickname"', formatter: Table.api.formatter.search },
                        { field: 'datatime', title: __('Datatime'), operate: 'RANGE', sortable: true, addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime, datetimeFormat: "YYYY-MM-DD" },
                        // {field: 'group.name', title: __('Group.name'), operate: false},
                        { field: 'subjects.name', title: __('Subjects.name'), operate: false },
                        { field: 'type.name', title: __('Type.name'), operate: false },
                        { field: 'admin.nickname', title: __('Admin.nickname'), operate: false },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', sortable: true, addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        //{field: 'audit.nickname', title: __('Audit_id'), operate: false},
                        //{field: 'audittime', title: __('Audittime'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        { field: 'amount', title: __('Amount'), operate: 'BETWEEN', sortable: true },
                        { field: 'desc', title: __('Desc'), operate: false },
                        { field: 'status', title: __('Status'), searchList: { "unaudited": __('Unaudited'), "audited": __('Audited') }, formatter: Table.api.formatter.status },
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    text: '编辑',
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    icon: 'fa fa-pencil',
                                    refresh: true,
                                    visible: function (e) {
                                        if (e.status == "unaudited")
                                            return !0
                                    },
                                },
                                {
                                    name: 'del',
                                    text: '删除记录',
                                    classname: 'btn btn-xs btn-delone',
                                    dropdown: '更多操作',//如果包含dropdown，将会以下拉列表的形式展示
                                    hidden: function (row) {
                                        return row.status == 'audited' ? true : false;
                                    },
                                    refresh: true
                                },
                                {
                                    name: 'copy',
                                    text: '复制记录',
                                    classname: 'btn btn-xs btn-dialog',
                                    dropdown: '更多操作',//如果包含dropdown，将会以下拉列表的形式展示
                                    url: $.fn.bootstrapTable.defaults.extend.add_url,
                                },
                                {
                                    name: 'click',
                                    text: '通过审核',
                                    title: '通过审核',
                                    classname: 'btn btn-xs btn-click',
                                    dropdown: '更多操作',//如果包含dropdown，将会以下拉列表的形式展示
                                    confirm: '确定要通过审核吗？',
                                    hidden: function (row) {
                                        return row.status == 'audited' ? true : false;
                                    },
                                    visible: function (e) {
                                        if (groupId != 3)
                                            return !0
                                    },
                                    click: function (data, row) {
                                        Fast.api.ajax({
                                            url: $.fn.bootstrapTable.defaults.extend.multi_url,
                                            data: { ids: row.id, params: "status=audited" },
                                            success: function (ret) {
                                                var msg = typeof ret.msg !== 'undefined' && ret.msg ? ret.msg : __('Operation completed');
                                                Toastr.success(msg);
                                                setTimeout("location.reload()", 1000); // 刷新
                                                return false;
                                            }
                                        });
                                    }
                                },
                                {
                                    name: 'click',
                                    text: '撤销审核',
                                    title: '撤销审核',
                                    classname: 'btn btn-xs btn-click',
                                    dropdown: '更多操作',//如果包含dropdown，将会以下拉列表的形式展示
                                    confirm: '确定要撤销审核吗？',
                                    hidden: function (row) {
                                        return row.status != 'audited' ? true : false;
                                    },
                                    visible: function (e) {
                                        if (groupId == 3)
                                            return !0
                                    },
                                    click: function (data, row) {
                                        Fast.api.ajax({
                                            url: $.fn.bootstrapTable.defaults.extend.multi_url,
                                            data: { ids: row.id, params: "status=unaudited" },
                                            success: function (ret) {
                                                var msg = typeof ret.msg !== 'undefined' && ret.msg ? ret.msg : __('Operation completed');
                                                Toastr.success(msg);
                                                setTimeout("location.reload()", 1000); // 刷新
                                                return false;
                                            }
                                        });
                                    }
                                },
                                // {
                                //     name: 'ajax',
                                //     title: __('发送Ajax'),
                                //     classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                //     icon: 'fa fa-magic',
                                //     confirm: '确认发送Ajax请求？',
                                //     url: 'finance/spendinglog/spending',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                //         //如果需要阻止成功提示，则必须使用return false;
                                //         //return false;
                                //     },
                                //     error: function (data, ret) {
                                //         console.log(data, ret);
                                //         Layer.alert(ret.msg);
                                //         return false;
                                //     }
                                // },

                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'finance/spendinglog/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                search: true,
                showToggle: false,
                fixedColumns: true,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), operate: false, sortable: true },
                        { field: 'amount', title: __('Amount'), operate: 'BETWEEN', sortable: true },
                        { field: 'desc', title: __('Desc'), operate: false },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'finance/spendinglog/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'finance/spendinglog/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});